#pragma once
#include "base/core/os/os_head.h"
#include <stdlib.h>
#include <stdint.h>
#include "base/net/callback.h"

#ifndef WIN32 //OS_LINUX
typedef  int  SOCKET;
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif  // OS_LINUX

struct NetInit 
{
	int        max_conn;
	uint32_t   timeout_ms;
	ICallback*  callback;

	NetInit()
		: max_conn(0),
		timeout_ms(0),
		callback(NULL) 
	{}
};

struct ConnectPeer 
{
    char*       ip;
    uint16_t    port;
    uint32_t    sendBufSize;
    uint32_t    recvBufSize;
    void*       req;
    void*       context;
    Subpackage  sub_func;

    ConnectPeer() 
	{
        ip = NULL;
        port = 0;
        sendBufSize = 0;
        recvBufSize = 0;
        req = NULL;
        context = NULL;
        sub_func = NULL;
    }
};

class Net 
{
public:
	Net() {}
	virtual ~Net() {}

	virtual bool  Init(const NetInit& init_data) = 0;
	virtual void  UnInit() = 0;
	virtual bool  Loop(bool poolWrite, bool poolRead, uint64_t timeout_ms) = 0;
	virtual bool  Poll(bool poolWrite, bool poolRead, uint64_t timeout_ms) = 0;

	virtual int   Close(ISession* isession) = 0;
	virtual bool  CloseListen(void* handle) = 0;
	virtual void* Listen(const ConnectPeer* peer) = 0;
	virtual bool  Connect(const ConnectPeer* peer) = 0;
	virtual bool  ResetTimeout(ISession* isession, uint32_t tm = 0xFFFFFFFF) = 0;
	virtual bool  Send(ISession* isession, const char* buf, uint32_t len, bool raw = false) = 0;

	virtual uint64_t GetSendedLength() = 0;
	virtual uint32_t GetAcceptSessionCount() = 0;
	virtual uint32_t GetConnectSessionCount() = 0;
};

Net*  CreateNet();
void  DeleteNet(Net* net);
